<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Get the content of the sina weibo of specific association or sb.
 * 
 * 获取新浪微博相关内容.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.1
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_util.php';
require_once INSTALLDIR . '/lib/slh_widget.php';


class Weibo extends Widget
{
	/**
	 * Id of the weibo.
	 */
	var $weibo_id;
	
	/**
	 * Nickname of the weibo.
	 */
	var $weibo_nickname;
	
	/**
	 * Constructor.
	 */
	function __construct($out,
						 $weibo_id,
						 $weibo_nickname)
	{
		parent::__construct($out);
		$this->out = $out;
		$this->weibo_id = $weibo_id;
		$this->weibo_nickname = $weibo_nickname;
	}
	
	/**
	 * Show the weibo.
	 */
	function show()
	{
		$this->out->elementStart('div', 'weiboheader');
		$this->out->element('img', array('src' => slh_full_path('theme/base/common/weibologo.png')));
		$this->out->elementStart('a', array('href' => 'http://t.sina.com.cn/' .  $this->weibo_id,
									   		'target' => '_blank'));
		$this->out->text('@厦门大学' . $this->weibo_nickname);
		$this->out->elementEnd('a');
		$this->out->element('img', array('class' => 'weiboloader',
										 'src' => slh_full_path('theme/base/common/loading.gif')));
		$this->out->element('hr');
		$this->out->elementEnd('div');
		
		$this->out->hidden('id_weibo_id', $this->weibo_id);
		$this->out->hidden('id_getweibohtmlaction', slh_action2url('getweibohtml'));
		
		$this->out->elementStart('div', 'weibo');
		$this->out->script('widget/weibo.go.js');
		$this->out->elementEnd('div');
	}
	
	/**
	 * Get the content from the sina weibo.
	 * 
	 * FIXME: More formatted style.
	 */
	static function getWeibo($weibo_id, 
							 $cnt = 5)
	{
		$output  = '<ul>';

		// 这里从一个新浪小组件里调用数据来源可以减少一定的数据流也可以避免跳到登陆页
		$contents  = file_get_contents('http://v.t.sina.com.cn/widget/widget_blog.php?uid='. $weibo_id .'');
		$tempItemContent  = explode('<p class="wgtCell_txt">', $contents);
		$tempItemTime  = explode('<span class="wgtCell_tm">', $contents);
		for($n = 1; $n <= $cnt; $n++) {
			$content  = explode('</p>', $tempItemContent[$n]);
			$time  = explode('</span>', $tempItemTime[$n]);
			$output .= '<li>' . $content[0] . '<p>' . $time[0] . '</p>' . '</li>';
		}
		
		$output .= '</ul>';
		
		return $output;
	}
};

?>